package com.alongthink.javademo.globalcatch;

/**
 * 自定义异常类
 */
public class AlongException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected String errorCode;
    /**
     * 错误信息
     */
    protected String errorMsg;

    public AlongException() {
        super();
    }

    public AlongException(BaseInfo errorInfoInterface) {
        super(errorInfoInterface.getResultCode());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public AlongException(BaseInfo errorInfoInterface, Throwable cause) {
        super(errorInfoInterface.getResultCode(), cause);
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public AlongException(String errorMsg) {
        super(errorMsg);
        this.errorMsg = errorMsg;
    }

    public AlongException(String errorCode, String errorMsg) {
        super(errorCode);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public AlongException(String errorCode, String errorMsg, Throwable cause) {
        super(errorCode, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }


    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getMessage() {
        return errorMsg;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

//    public void writeMsg(ResultBody resultBody, HttpServletResponse httpServletResponse) throws IOException {
//        httpServletResponse.setContentType("application/json;charset=utf-8");
//        PrintWriter out = httpServletResponse.getWriter();
//        StringBuffer sb = new StringBuffer();
//        sb.append("{\"status\":\"error\",\"msg\":\"");
//        if (e instanceof UsernameNotFoundException || e instanceof BadCredentialsException) {
//            sb.append("用户名或密码输入错误，登录失败!");
//        } else if (e instanceof DisabledException) {
//            sb.append("账户被禁用，登录失败，请联系管理员!");
//        } else {
//            sb.append("登录失败!");
//        }
//        sb.append("\"}");
//        out.write(sb.toString());
//        out.flush();
//        out.close();
//    }
}
